package OSdesign.GUI_show;

import OSdesign.Function_show.MatchManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Menu_one extends JFrame {
    private JPanel addPanel;
    private JPanel ediPanel;
    private JPanel applyPanel;
    private JTextField numField;
    private JTextField nameField;
    private JTextField ruleField;
    private JTextField recordField;
    private JTextArea ruleArea;
    public Menu_one(){
        //构造函数
    }
    public JFrame getJFrame(){
        return this;
    }
    public void addView(){
        String[] strs = new String[4];
        Font font = new Font("",Font.BOLD,20);
        addPanel = new JPanel();
        addPanel.setBounds(0,0,500,600);
        addPanel.setLayout(null);

        JLabel numLabel = new JLabel("比赛编号");
        numLabel.setFont(font);
        numLabel.setBounds(50,50,200,50);
        JLabel nameLabel = new JLabel("比赛名称");
        nameLabel.setFont(font);
        nameLabel.setBounds(50,130,200,50);
        JLabel ruleLabel = new JLabel("比赛规则");
        ruleLabel.setFont(font);
        ruleLabel.setBounds(50,210,200,50);
        JLabel recordLabel = new JLabel("比赛最好记录");
        recordLabel.setFont(font);
        recordLabel.setBounds(50,290,200,50);

        numField = new JTextField();
        numField.setFont(font);
        numField.setBounds(200,50,200,50);
        nameField = new JTextField();
        nameField.setFont(font);
        nameField.setBounds(200,130,200,50);
        ruleField = new JTextField();
        ruleField.setFont(font);
        ruleField.setBounds(200,210,200,50);
        recordField = new JTextField();
        recordField.setFont(font);
        recordField.setBounds(200,290,200,50);

        //确定添加按钮
        JButton button = new JButton("确定");
        button.setFont(font);
        button.setBounds(120,430,100,50);
        //按钮监听器，添加一条比赛信息
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //将文本框得到的字符串用  字符串数组存放
                strs[0]=numField.getText();
                strs[1]=nameField.getText();
                strs[2]=ruleField.getText();
                strs[3]=recordField.getText();
                if(new MatchManager().addCompetition(strs)){
                    JOptionPane.showMessageDialog(null,"添加成功！");
                    numField.setText("");
                    nameField.setText("");
                    ruleField.setText("");
                    recordField.setText("");
                }else{
                    JOptionPane.showMessageDialog(null,"添加失败！");
                }
            }
        });
        //添加返回按钮
        JButton unbutton = new JButton("返回");
        unbutton.setFont(font);
        unbutton.setBounds(270,430,100,50);
        unbutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                getJFrame().dispose();
            }
        });
        //组件添加到 panel 面板
        addPanel.add(numLabel);
        addPanel.add(nameLabel);
        addPanel.add(ruleLabel);
        addPanel.add(recordLabel);
        addPanel.add(numField);
        addPanel.add(nameField);
        addPanel.add(ruleField);
        addPanel.add(recordField);
        addPanel.add(button);
        addPanel.add(unbutton);
        //panel添加到frame窗口
        this.setLayout(null);
        this.setBounds(600,250,500,600);
        this.setFocusable(false);
        this.add(addPanel);
        this.setTitle("添加比赛");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void ediView(){
        String[] data = new String[2];  //存储新修改的比赛规则
        Font font = new Font("",Font.BOLD,20);
        ediPanel = new JPanel();
        ediPanel.setLayout(null);
        ediPanel.setBounds(0,0,500,500);
        //标签组件
        JLabel numLabel = new JLabel("比赛编号");
        numLabel.setFont(font);
        numLabel.setBounds(50,50,200,50);
        JLabel ruleLabel = new JLabel("比赛规则");
        ruleLabel.setFont(font);
        ruleLabel.setBounds(50,150,200,50);
        //文本域组件
        numField = new JTextField();
        numField.setFont(font);
        numField.setBounds(150,50,200,50);
        ruleArea = new JTextArea();
        ruleArea.setFont(font);
        ruleArea.setLineWrap(true);
        ruleArea.setBounds(50,200,350,150);
        JScrollPane scrollPane = new JScrollPane(ruleArea);
        scrollPane.setBounds(50,200,350,150);

        //按钮组件
        JButton button = new JButton("确定");
        button.setFont(font);
        button.setBounds(100,400,100,50);
        //按钮监听器，添加一条比赛信息
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //将文本框得到的字符串用  字符串存放
                data[0]=numField.getText();
                data[1]=ruleArea.getText();
                if(new MatchManager().ediRule(data)){
                    JOptionPane.showMessageDialog(null,"修改成功！");
                    getJFrame().dispose();
                }else{
                    JOptionPane.showMessageDialog(null,"添加失败！");
                    ruleArea.setText("");
                }

            }
        });

        //添加返回按钮
        JButton unbutton = new JButton("返回");
        unbutton.setFont(font);
        unbutton.setBounds(250,400,100,50);
        unbutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                getJFrame().dispose();
            }
        });
        //ediPanel 添加组件
        ediPanel.add(numLabel);
        ediPanel.add(numField);
        ediPanel.add(ruleLabel);
        ediPanel.add(scrollPane);
        ediPanel.add(button);
        ediPanel.add(unbutton);
        //panel添加到frame窗口
        this.setLayout(null);
        this.setBounds(600,250,500,500);
        this.setFocusable(false);
        this.add(ediPanel);
        this.setTitle("修改比赛规则");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void applyView(){
        String[] strs = new String[5];
        Font font = new Font("",Font.BOLD,20);
        applyPanel = new JPanel();
        applyPanel.setBounds(0,0,500,600);
        applyPanel.setLayout(null);

        JLabel numLabel = new JLabel("运动员编号");
        numLabel.setFont(font);
        numLabel.setBounds(50,50,200,50);
        JLabel nameLabel = new JLabel("运动员姓名");
        nameLabel.setFont(font);
        nameLabel.setBounds(50,120,200,50);
        JLabel genderLabel = new JLabel("性别");
        genderLabel.setFont(font);
        genderLabel.setBounds(50,190,200,50);
        JLabel teamLabel = new JLabel("所属团队");
        teamLabel.setFont(font);
        teamLabel.setBounds(50,260,200,50);
        JLabel matchLabel = new JLabel("参赛名称");
        matchLabel.setFont(font);
        matchLabel.setBounds(50,330,200,50);


        numField = new JTextField();
        numField.setFont(font);
        numField.setBounds(200,50,200,50);
        nameField = new JTextField();
        nameField.setFont(font);
        nameField.setBounds(200,120,200,50);
        JTextField genderField = new JTextField();
        genderField.setFont(font);
        genderField.setBounds(200,190,200,50);
        JTextField teamField = new JTextField();
        teamField.setFont(font);
        teamField.setBounds(200,260,200,50);
        JTextField matchField = new JTextField();
        matchField.setFont(font);
        matchField.setBounds(200,330,200,50);

        //确定添加按钮
        JButton button = new JButton("确定");
        button.setFont(font);
        button.setBounds(120,430,100,50);
        //按钮监听器，添加一条比赛信息
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                //将文本框得到的字符串用  字符串数组存放
                strs[0]=numField.getText();
                strs[1]=nameField.getText();
                strs[2]=genderField.getText();
                strs[3]=teamField.getText();
                strs[4]=matchField.getText();
                if(new MatchManager().applyMatch(strs)){
                    JOptionPane.showMessageDialog(null,"添加成功！");
                    numField.setText("");
                    nameField.setText("");
                    genderField.setText("");
                    teamField.setText("");
                    matchField.setText("");
                }else{
                    JOptionPane.showMessageDialog(null,"添加失败");
                }
            }
        });

        //添加返回按钮
        JButton unbutton = new JButton("返回");
        unbutton.setFont(font);
        unbutton.setBounds(270,430,100,50);
        unbutton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                getJFrame().dispose();
            }
        });

        //组件添加到 panel 面板
        applyPanel.add(numLabel);
        applyPanel.add(nameLabel);
        applyPanel.add(genderLabel);
        applyPanel.add(teamLabel);
        applyPanel.add(matchLabel);
        applyPanel.add(numField);
        applyPanel.add(nameField);
        applyPanel.add(genderField);
        applyPanel.add(teamField);
        applyPanel.add(matchField);
        applyPanel.add(button);
        applyPanel.add(unbutton);

        //panel添加到frame窗口
        this.setLayout(null);
        this.setBounds(600,250,500,600);
        this.setFocusable(false);
        this.add(applyPanel);
        this.setTitle("运动员录入");
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args){
        new Menu_one().applyView();
    }
}
